package com.zerosoft.guotai.dao;

import lombok.Data;

import java.nio.ByteBuffer;

/**
 * 国泰G7设备事件数据结构
 * @author guxuefeng
 * 字节	    长度	格式	    范围	        项目	                Name
 * 0	    1	HEX	    1 ~ 255	    事件版本号；本版值为1	EventVersion
 * 1	    1	HEX	    0 ~ 254	    事件代码	            EventCode
 * 2,3	    2	HEX	    1 ~1023	    机号	                EventControllerID
 * 4	    1	HEX	    1 ~ 64	    回路、盘号	        EventLoopPanelID
 * 5	    1	HEX	    1 ~ 255	    地址号	            EventAddress
 * 6	    1	HEX	    1 ~ 16	    通道号	            EventChannel
 * 7	    1			            预留	                EventReserve1
 * 8	    1	HEX	    0 ~ 255	    设备类型	            EventDeviceType
 * 9	    1	HEX		            盘卡类型	            PanelCardType
 * 10,11	2	HEX		            报警值	            EventAlarmValue
 * 12,13	2			            预留	                EventReserve3[2]
 * 14,15	2	HEX	    0 ~ 1023	楼	                EventBuilding
 * 16	    1	HEX	    -9 ~ 99	    层	                EventFloor
 * 17	    1	HEX	    0 ~ 255	    区	                EventZone
 * 18	    1	HEX 	0 ~ 255	    年	                EventYear
 * 19	    1	HEX	    1 ~ 12	    月	                EventMonth
 * 20	    1	HEX	    1 ~ 31	    日	                EventDay
 * 21	    1	HEX	    0 ~ 23	    时	                EventHour
 * 22	    1	HEX	    0 ~ 59	    分	                EventMinute
 * 23	    1	HEX	    0 ~ 59	    秒	                EventSecond
 * 24～63	40	字符串		    说明文字，可配置40个字符	EventCaption[40]
 */
@Data
public class G7EventInfo {
    private byte version;
    private byte code;
    private short controllerId;
    private byte loopPanelId;
    private byte address;
    private byte channel;
    private byte reserved;
    private byte deviceType;
    private byte panelType;
    private short alarmValue;
    private short reservedValue;
    private short building;
    private byte floor;
    private byte zone;
    private byte year;
    private byte month;
    private byte day;
    private byte hour;
    private byte minute;
    private byte second;
    private String description;

//    @Override
//    public String toString() {
//        return "MyObject{" +
//                "id=" + id +
//                ", name='" + name + '\'' +
//                ", value=" + value +
//                '}';
//    }
//      用法，在调用的地方使用
//    // 假设这是从某个地方获取的定长缓冲区数据
//    byte[] buffer = new byte[32];
//    // 填充缓冲区数据
//    ByteBuffer bb = ByteBuffer.wrap(buffer);
//        bb.putInt(1); // id
//        bb.put("Example Name".getBytes(StandardCharsets.UTF_8)); // name
//        bb.putDouble(123.456); // value
//
//    // 转换为 MyObject
//    MyObject myObject = BufferToObjectConverter.convert(buffer);
//        System.out.println(myObject);

    //TODO:增加转换为GuotaiEventParse的方法
    public GuotaiEventParse convertToGuotaiEventParse(){
        GuotaiEventParse guotaiEventParse = new GuotaiEventParse();
        guotaiEventParse.setDeviceType(deviceType & 0xFF);
        guotaiEventParse.setEventType(code & 0xFF);
        guotaiEventParse.setDate(String.format("%02d-%02d-%02d",year,month,day));
        guotaiEventParse.setTime(String.format("%02d:%02d:%02d",hour,minute,second));
        guotaiEventParse.setBuild(String.format("%02d楼%03d层%03d区",building,floor,zone));
        guotaiEventParse.setMachine(String.format("%d%d机",controllerId&0xFF,controllerId>>8 & 0xFF));
        guotaiEventParse.setEventNumber(String.format("%02d回路%03d号",loopPanelId,address));

        guotaiEventParse.setDescription(
//                G7DeviceEnum.getMessage(deviceType & 0xFF) +
//                G7DeviceEnum.getMessage(panelType & 0xFF) +
                G7EventEnum.getMessage(code & 0xFF) +
                String.format("，数值：%04d。%s" , alarmValue,description));

        guotaiEventParse.setPanelTypeName(G7DeviceEnum.getMessage(panelType & 0xFF));

        guotaiEventParse.setPartTypeName(G7DeviceEnum.getMessage(deviceType & 0xFF));

        guotaiEventParse.setRoad(String.format("%02d通道",channel));

        guotaiEventParse.setPosition(String.format("%s%s",guotaiEventParse.getEventNumber(),guotaiEventParse.getRoad()));
        return guotaiEventParse;
    }
}
